let common = require("../../utils/formatCurrency");
const app = getApp();
import * as echarts from '../../ec-canvas/echarts';

Page({
  data: {
    ec: {
      lazyLoad: true 
    },
    chartOrform: true,
    fixedText: '列表',
    profitList: [{
      "item": "一、营业收入",
      "row": "1",
      "yearCash": "165165",
      "monthCash": "5959",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：主营业务收入+其他业务收入",
    }, {
      "item": "减：营业成本",
      "row": "2",
      "yearCash": "2652",
      "monthCash": "626",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：主营业务成本+其他业务成本",
    }, {
      "item": "营业税金及附加",
      "row": "3",
      "yearCash": "-2626",
      "monthCash": "15",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：营业税金及附加",
      "open": "1",
      "closeNum": "10",
      "openStatus": true
    }, {
      "item": "　其中：消费税",
      "row": "4",
      "yearCash": "651476",
      "monthCash": "-26969",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：应交消费税",
    }, {
      "item": "　　　营业税",
      "row": "5",
      "yearCash": "-6621",
      "monthCash": "-445",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：应交营业税",
    }, {
      "item": "　　　城市维护建设税",
      "row": "6",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：应交城市维护建设税",
    }, {
      "item": "　　　资源税",
      "row": "7",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：应交资源税",
    }, {
      "item": "　　　土地增值税",
      "row": "8",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：应交土地增值税",
    }, {
      "item": "　　　城镇土地使用税、房产税、车船税、印花税",
      "row": "9",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：应交城镇土地使用税+应交房产税+应交车船使用税",
    }, {
      "item": "　　　教育费附加、矿产资源补偿费、排污费",
      "row": "10",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：地方教育费附加+教育费附加+矿产资源补偿费+排污费",
    }, {
      "item": "销售费用",
      "row": "11",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：销售费用",
      "open": "1",
      "closeNum": "13",
      "openStatus": true
    }, {
      "item": "　其中：商品维修费",
      "row": "12",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：商品维修费",
    }, {
      "item": "　　　广告费和业务宣传费",
      "row": "13",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：广告费+业务宣传费",
    }, {
      "item": "管理费用",
      "row": "14",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：管理费用",
      "open": "1",
      "closeNum": "17",
      "openStatus": true
    }, {
      "item": "　其中：开办费",
      "row": "15",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：开办费",
    }, {
      "item": "　　　业务招待费",
      "row": "16",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：业务招待费",
    }, {
      "item": "　　　研究费用",
      "row": "17",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：研究费用",
    }, {
      "item": "财务费用",
      "row": "18",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：财务费用",
      "open": "1",
      "closeNum": "19",
      "openStatus": true
    }, {
      "item": "　其中：利息费用（收入以“-”号填列）",
      "row": "19",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：利息费用",
    }, {
      "item": "加：投资收益（损失以“-”号填列）",
      "row": "20",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：投资收益",
    }, {
      "item": "二、营业利润（亏损以“-”号填列）",
      "row": "21",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "行次：行1-行2-行3-行11-行14-行18+行20",
    }, {
      "item": "加：营业外收入",
      "row": "22",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：营业外收入",
      "open": "1",
      "closeNum": "23",
      "openStatus": true
    }, {
      "item": "　其中：政府补助",
      "row": "23",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：政府补助",
    }, {
      "item": "减：营业外支出",
      "row": "24",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：营业外支出",
      "open": "1",
      "closeNum": "29",
      "openStatus": true
    }, {
      "item": "　其中：坏账损失",
      "row": "25",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：坏账损失",
    }, {
      "item": "　　　无法收回的长期债券投资损失",
      "row": "26",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：无法收回的长期债券投资损失",
    }, {
      "item": "　　　无法收回的长期股权投资损失",
      "row": "27",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：无法收回的长期股权投资损失",
    }, {
      "item": "　　　自然灾害等不可抗力因素造成的损失",
      "row": "28",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：自然灾害等不可抗力因素造成的损失",
    }, {
      "item": "　　　税收滞纳金",
      "row": "29",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：税收滞纳金",
    }, {
      "item": "三、利润总额（亏损总额以“-”号填列）",
      "row": "30",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "行次：行21+行22-行24",
    }, {
      "item": "减：所得税费用",
      "row": "31",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "科目：所得税费用",
    }, {
      "item": "四、净利润（净亏损以“-”号填列）",
      "row": "32",
      "yearCash": "0",
      "monthCash": "0",
      "lastYearMonthCash": "0",
      "fomularDetail": "行次：行30-行31",
    }],
    list:[
      { text:'收入',money:'16500'}, 
      { text: '成本', money: '16500'}, 
      { text: '费用', money: '16500'}, 
      { text: '税金及附加', money: '16500' }, 
      { text: '净利润', money: '16500'}
    ],
    optionData:[],
    total:'',
    showButton: true,
    height:'600rpx',
    date:'',
    isChart: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    common.handleNavigationBar(app.globalData.company);

    this.data.date = options.data;
    let that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.url + 'api/profits/find',
      method: 'POST',
      header: {
        token: app.globalData.token,
        version: app.globalData.version
      },
      data: {
        accountBookId: app.globalData.accountBookId,
        endPeriod: options.data
      },
      success: function (res) {
        that.handleData(res.data)
        that.setData({
          isChart: true
        })
      }
    })
  },

  handleData(data){
    for (let i = 0; i < this.data.profitList.length;i++){
      this.data.profitList[i].monthCash = Number(data.monthCashs[i]);
      this.data.profitList[i].yearCash = Number(data.yearCashs[i]);
    }
      
    this.data.list[0].money = Number(data.monthCashs[0]);
    this.data.list[1].money = Number(data.monthCashs[1]);
    this.data.list[2].money = Number(data.monthCashs[10]) + Number(data.monthCashs[13]) + Number(data.monthCashs[17]);
    this.data.list[3].money = Number(data.monthCashs[2]);
    this.data.list[4].money = Number(data.monthCashs[31]);

    let monthCashTotal=0;
    for (let i = 0; i < this.data.profitList.length; i++) {
      monthCashTotal += this.data.profitList[i].monthCash
    }

    

    if (monthCashTotal<=0){
      wx.hideLoading()
      this.setData({
        showButton: false,
        chartOrform: false,
        profitList: this.data.profitList,
        height: '0rpx'
      })
      return
    }

    for (let i = 0; i < this.data.list.length; i++) {
      wx.hideLoading()
      if (this.data.list[i].money<0){
        this.setData({
          showButton: false,
          chartOrform: false,
          profitList: this.data.profitList,
          height: '0rpx'
        })
        return
      }
    }

    this.data.total = Number(data.monthCashs[0]) + Number(data.monthCashs[1]) + Number(data.monthCashs[10]) + Number(data.monthCashs[13]) + Number(data.monthCashs[17]) + Number(data.monthCashs[2]) + Number(data.monthCashs[31])
    

    for(let i=0;i<this.data.list.length;i++){
      let num = Number(this.data.list[i].money);
      let percent = (num/this.data.total*100).toFixed(2);
      this.data.optionData.push({
        value: this.data.list[i].money, 
        name: this.data.list[i].text + percent+'%'
      })
    }

    this.data.total = common.currency(this.data.total)

    // 获取组件 初始化图表
    this.selectComponent('#mychart-dom-bar').init((canvas, width, height) => {
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      this.loadChart(chart);
      return chart;
    });

    this.setData({
      profitList: this.data.profitList,
      list: this.data.list
    })
    wx.hideLoading()
  },

  swich(){
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
    if (this.data.chartOrform){
      this.setData({
        chartOrform: false,
        fixedText: '图表',
        height: '0rpx'
      })
    }else{
      this.setData({
        chartOrform: true,
        fixedText: '列表',
        height: '600rpx'
      })
    }
  },
  loadChart(chart){
    let silent = app.globalData.platform.toLowerCase() === 'android' ? true : false
    var option = {
      graphic: [
        {
          type: 'text',
          left: 'center',
          top: '44%',
          style: {
            text: '本年累计',
            textAlign: 'center',
            fill: '#8a8a8a',
            fontSize: 12
          },
        },
        {
          type: 'text',
          left: 'center',
          top: '52%',
          style: {
            text: this.data.total,
            textAlign: 'center',
            fontSize: 16
          }
        },
      ],
      series: [
        {
          name: '访问来源',
          type: 'pie',
          radius: ['45%', '70%'],
          data: this.data.optionData,
          minAngle: 3,
          silent: silent,
          itemStyle: {
            emphasis: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          },
          labelLine: {
            length: 10,
            length2: 10
          },
          color: ['#3d731b', '#3283bb', '#529fd3', '#7addfa', '#89ce33']
        }
      ]
    };
    chart.setOption(option);
  },

  downLoad(){
    let url = app.globalData.url + 'api/export/pdf/income?accountBookId=' + app.globalData.accountBookId + '&startPeriod=' + this.data.date + '&endPeriod=' + this.data.date + '&version=' + app.globalData.version + '&token=' + app.globalData.token
    common.download(url)
  }
})